package com.yy.train.ticket.train.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.train.ticket.into.LineLink;
import com.yy.train.ticket.train.dao.TtLineLinkDao;
import com.yy.train.ticket.train.dao.TtLineLinkSectionDao;
import com.yy.train.ticket.train.dao.TtLineSectionDao;
import com.yy.train.ticket.train.dao.TtLineStationDao;
import com.yy.train.ticket.train.entity.TtLineLinkEntity;
import com.yy.train.ticket.train.entity.TtLineLinkSectionEntity;
import com.yy.train.ticket.train.entity.TtLineSectionEntity;
import com.yy.train.ticket.train.entity.TtLineStationEntity;
import com.yy.train.ticket.train.service.TtLineLinkService;
import com.yy.train.ticket.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("ttLineLinkService")
public class TtLineLinkServiceImpl extends ServiceImpl<TtLineLinkDao, TtLineLinkEntity> implements TtLineLinkService {
 @Resource
        TtLineLinkDao ttLineLinkDao ;
 @Resource
    TtLineLinkSectionDao ttLineLinkSectionDao;
    @Resource
    TtLineStationDao ttLineStationDao;
    @Resource
    TtLineSectionDao ttLineSectionDao;
    @Override
    public R assemble(TtLineLinkEntity ttLineLink) {
        Long lineId = ttLineLink.getLineId();
        Long aLong = this.ttLineLinkDao.selectCount(Wrappers.<TtLineLinkEntity>lambdaQuery().eq(TtLineLinkEntity::getLineId, lineId));
        if (aLong == 0L) {


        List<TtLineStationEntity> li1 = ttLineStationDao.selectList(Wrappers.<TtLineStationEntity>lambdaQuery()
                .eq(TtLineStationEntity::getLineId,lineId));
        List<TtLineSectionEntity> li = ttLineSectionDao.selectList(Wrappers.<TtLineSectionEntity>lambdaQuery()
                .eq(TtLineSectionEntity::getLineId,lineId).orderByAsc(TtLineSectionEntity::getStartStationId));
        Map<Long, List<TtLineSectionEntity>> map = li.stream().collect(Collectors.groupingBy(TtLineSectionEntity::getStartStationId));
        Long startStationId = li1.get(0).getStationId();
        Long endStationId = li1.get(li1.size() - 1).getStationId();
        TtLineSectionEntity [] lineSectionlist =new TtLineSectionEntity[li1.size()];
        List<TtLineSectionEntity []> list=new ArrayList<>();
        extracted(startStationId,endStationId, map, lineSectionlist,list,0);
        int n=0;
        for (TtLineSectionEntity [] ls:list
             ) {
            TtLineLinkEntity lineLink=new TtLineLinkEntity();
            lineLink.setLineId(lineId);
            lineLink.setLinkName("L--"+(n++));
            this.ttLineLinkDao.insert(lineLink);
            for (int i = 0; i < ls.length; i++) {
                TtLineSectionEntity s=ls[i];
                TtLineLinkSectionEntity ttLineLinkSectionEntity=new TtLineLinkSectionEntity();
                ttLineLinkSectionEntity.setLineId(lineId);
                ttLineLinkSectionEntity.setLinkId(lineLink.getLinkId());
                ttLineLinkSectionEntity.setSectionId(s.getSectionId());
                this.ttLineLinkSectionDao.insert(ttLineLinkSectionEntity);
            }

        }
        return R.ok();
        }else {
            return R.error("已经生成了线路段，不用重新生成");
        }
    }

    @Override
    public R linelinklist(Long lineId) {
     List<LineLink> list=   ttLineLinkDao.linelinklist(lineId);
        return R.ok(list);
    }

    @Override
    public R deleteLineLinkByIds(List<Long> list) {
        this.ttLineLinkSectionDao.delete(Wrappers.<TtLineLinkSectionEntity>lambdaUpdate().in(TtLineLinkSectionEntity::getLinkId,list));
        this.ttLineLinkDao.deleteBatchIds(list);
        return R.ok();
    }

    private static void extracted(Long staId,Long endStaId, Map<Long, List<TtLineSectionEntity>> map,  TtLineSectionEntity [] lineSectionlist,  List<TtLineSectionEntity []> list,int n) {
        for (Map.Entry<Long, List<TtLineSectionEntity>> entry : map.entrySet()) {
            Long stationId = entry.getKey();
            if (Objects.equals(staId, stationId)) {
                for (int i = 0; i <entry.getValue().size() ; i++) {
                    TtLineSectionEntity ttLineSectionEntity = entry.getValue().get(i);
                    Long endStationId = ttLineSectionEntity.getEndStationId();
                    System.out.print(ttLineSectionEntity.getLineSectionName()+" -->  ");
                    lineSectionlist[n]=ttLineSectionEntity;
                    if (Objects.equals(endStaId, endStationId)) {
                        System.out.println("-----"+n);
                        TtLineSectionEntity [] string=new TtLineSectionEntity[n+1];
                        System.arraycopy(lineSectionlist, 0, string, 0, n + 1);
                        list.add(string);
                    }else {
                        extracted(endStationId,endStaId, map, lineSectionlist,list,n+1);
                    }
                }
            }
        }
    }

}